package com.xiaouyudeguang.common.request;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.xiaouyudeguang.common.exception.BusinessException;
import com.xiaouyudeguang.common.utils.JsonObject;
import com.xiaouyudeguang.common.utils.MapModel;
import lombok.*;

import java.util.Map;
import java.util.Set;

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RequestDto<T> {

    private Map<String, Object> param;

    private Set<String> ids;

    private Set<String> queryKeys;

    private T data;

    private Integer current = 1;

    private Integer size = 10;

    private String scope;

    private String startTime;

    private String endTime;

    private String asc;

    private String desc;

    private String keyword;

    private boolean force;

    @JsonIgnore
    private String dataId;

    @JsonIgnore
    private String roleId;

    @JsonIgnore
    private String deptId;

    @JsonIgnore
    private String userId;

    public T getData() throws Exception {
        if (data == null) {
            throw new BusinessException("参数异常");
        }
        return data;
    }

    @JsonIgnore
    public JsonNode getNode() throws Exception {
        if (data == null) {
            throw new BusinessException("参数异常");
        }
        return JsonObject.readTree(this);
    }

    @JsonIgnore
    public Page<T> getPage() throws Exception {
        return Page.of(current, size);
    }

    @JsonIgnore
    public RequestDto<T> getPage(Integer current, Integer size) {
        this.current = current;
        this.size = size;
        return this;
    }

    @JsonIgnore
    public QueryWrapper<T> getWrapper() throws Exception {
        if (data == null) {
            throw new BusinessException("参数异常");
        }
        return new QueryWrapper<>(data);
    }

    @JsonIgnore
    public LambdaQueryWrapper<T> getLambdaWrapper() throws Exception {
        if (data == null) {
            throw new BusinessException("参数异常");
        }
        return new LambdaQueryWrapper<>(data);
    }

    public MapModel<String, Object> getParam() throws Exception {
        return MapModel.get(param);
    }

    public String getParam(String key) throws Exception {
        return MapModel.get(param).getText(key);
    }

    public static <T> RequestDto<T> getRequest(T data) {
        return getRequest(data, null);
    }

    public static <T> RequestDto<T> getRequest(Map<String, Object> param) {
        return getRequest(null, param);
    }

    public static <T> RequestDto<T> getRequest(T data, Map<String, Object> param) {
        RequestDto<T> requestDto = new RequestDto<>();
        requestDto.setData(data);
        requestDto.setParam(param);
        return requestDto;
    }

    public static <T> RequestDto<T> getRequest(Integer current, Integer size) {
        RequestDto<T> requestDto = new RequestDto<>();
        requestDto.setCurrent(current);
        requestDto.setSize(size);
        return requestDto;
    }
}
